﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class web_Controls_ctrl_KIOS_kh : System.Web.UI.UserControl
{
    Common _common = null;
    public string sql_where = "";
    public static string _type = "";
    public static string _id = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        _common = new Common();
        if (!Page.IsPostBack)
        {
            if (Request.Params["type"] != null)
            {
                _type = Request.Params["type"].ToString();
                div_list_by_cat.Visible = true;
                if (_type.Trim().Equals("1"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH CƠ SỞ ĐÀO TẠO";
                    LoadCosodaotao();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadKhoadaotaoByCosodaotao(_id);
                    }
                }
                if (_type.Trim().Equals("2"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH NGÀNH NGHỀ ĐÀO TẠO";
                    LoadNganhdaotao();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadKhoadaotaoByNganhdaotao(_id);
                    }
                }
                if (_type.Trim().Equals("3"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH TRÌNH ĐỘ ĐÀO TẠO";
                    LoadTrinhdodaotao();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadKhoadaotaoByTrinhdo(_id);
                    }
                }
            }
            else
            {
                _type = "";
                LoadKhoadaotao();
            }
        }
    }

    void LoadKhoadaotao()
    {
        DataTable totalrow = null;
        if (sql_where.Trim().Equals(""))
        {
            totalrow = _common.getDataTable("select ma_khoa_dt from DaoTao_khoa_dao_tao ");
        }
        else { totalrow = _common.getDataTable("select ma_khoa_dt from DaoTao_khoa_dao_tao where " + sql_where); }
        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString();
        SqlParameter[] pa = new SqlParameter[6];

        pa[0] = new SqlParameter("@Tables", "DaoTao_khoa_dao_tao");
        pa[1] = new SqlParameter("@PK", "ngay_tao");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
        pa[4] = new SqlParameter("@Filter", sql_where);
        pa[5] = new SqlParameter("@Fields", "ma_khoa_dt, ten_khoa_dt, ngay_nhan_ho_so, ngay_dong_ho_so, ngay_khai_giang, hoc_phi, thoi_gian_hoc, convert(varchar, ngay_tao, 103) as ngay_tao_vn ");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }
    protected void pagination1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        pagination1.CurrentIndex = currnetPageIndx;
        if (_type.Trim().Equals(""))
        {
            LoadKhoadaotao();
        }
        else
        {
            if (_type.Trim().Equals("1"))
            {
                LoadKhoadaotaoByCosodaotao(_id);
            }
            if (_type.Trim().Equals("2"))
            {
                LoadKhoadaotaoByNganhdaotao(_id);
            }
            if (_type.Trim().Equals("3"))
            {
                LoadKhoadaotaoByTrinhdo(_id);
            }
        }
    }
    void LoadCosodaotao()
    {
        string _sql_cd = "select ma_co_so_dt as Ma, ten_co_so_dt as TEN from DaoTao_co_so_dao_tao where ma_co_so_dt IN (select ma_co_so_dt from DaoTao_khoa_dao_tao where kich_hoat = 1)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadKhoadaotaoByCosodaotao(_id);
            }
        }
    }
    void LoadNganhdaotao()
    {
        string _sql_cd = "select ma_nganh_kd as MA, ten_nganh_kd as TEN from DanhMuc_nganh_kinh_doanh where ma_nganh_kd IN (select ma_nganh_dt from DaoTao_khoa_dao_tao where kich_hoat = 1 group by ma_nganh_dt)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadKhoadaotaoByNganhdaotao(_id);
            }
        }
    }
    void LoadTrinhdodaotao()
    {
        string _sql_cd = "select ma_hoc_van as MA, ten_hoc_van as TEN from DanhMuc_hoc_van where ma_hoc_van IN (select ma_trinh_do_dt from DaoTao_khoa_dao_tao where kich_hoat = 1 group by ma_trinh_do_dt)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadKhoadaotaoByTrinhdo(_id);
            }
        }
    }


    void LoadKhoadaotaoByCosodaotao(string ma_co_so)
    {
        DataTable totalrow = null;
        totalrow = _common.getDataTable("select ma_khoa_dt from DaoTao_khoa_dao_tao as A inner join DaoTao_co_so_dao_tao as B on A.ma_co_so_dt = B.ma_co_so_dt where A.ma_co_so_dt = '" + ma_co_so + "'");

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString();
        SqlParameter[] pa = new SqlParameter[6];

        pa[0] = new SqlParameter("@Tables", " DaoTao_khoa_dao_tao as A inner join DaoTao_co_so_dao_tao as B on A.ma_co_so_dt = B.ma_co_so_dt ");
        pa[1] = new SqlParameter("@PK", "A.ngay_tao");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
        pa[4] = new SqlParameter("@Filter", " A.ma_co_so_dt = '" + ma_co_so + "'");
        pa[5] = new SqlParameter("@Fields", " A.ma_khoa_dt, A.ten_khoa_dt, A.ngay_nhan_ho_so, A.ngay_dong_ho_so, A.ngay_khai_giang, A.hoc_phi, A.thoi_gian_hoc, convert(varchar, A.ngay_tao, 103) as ngay_tao_vn ");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }

    void LoadKhoadaotaoByNganhdaotao(string ma_nganh)
    {
        DataTable totalrow = null;
        totalrow = _common.getDataTable("select A.ma_khoa_dt from DaoTao_khoa_dao_tao as A where A.ma_nganh_dt = '" + ma_nganh + "' and A.kich_hoat = 1");

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString();
        SqlParameter[] pa = new SqlParameter[6];

        pa[0] = new SqlParameter("@Tables", "  DaoTao_khoa_dao_tao as A ");
        pa[1] = new SqlParameter("@PK", "A.ngay_tao");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
        pa[4] = new SqlParameter("@Filter", " A.ma_nganh_dt = '" + ma_nganh + "' and A.kich_hoat = 1");
        pa[5] = new SqlParameter("@Fields", " A.ma_khoa_dt, A.ten_khoa_dt, A.ngay_nhan_ho_so, A.ngay_dong_ho_so, A.ngay_khai_giang, A.hoc_phi, A.thoi_gian_hoc, convert(varchar, A.ngay_tao, 103) as ngay_tao_vn  ");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }

    void LoadKhoadaotaoByTrinhdo(string ma_trinh_do)
    {
        DataTable totalrow = null;
        totalrow = _common.getDataTable("select A.ma_khoa_dt from DaoTao_khoa_dao_tao as A where A.ma_trinh_do_dt = '" + ma_trinh_do + "' and A.kich_hoat = 1");

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString();
        SqlParameter[] pa = new SqlParameter[6];

        pa[0] = new SqlParameter("@Tables", "  DaoTao_khoa_dao_tao as A ");
        pa[1] = new SqlParameter("@PK", "A.ngay_tao");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
        pa[4] = new SqlParameter("@Filter", " A.ma_trinh_do_dt = '" + ma_trinh_do + "' and A.kich_hoat = 1");
        pa[5] = new SqlParameter("@Fields", " A.ma_khoa_dt, A.ten_khoa_dt, A.ngay_nhan_ho_so, A.ngay_dong_ho_so, A.ngay_khai_giang, A.hoc_phi, A.thoi_gian_hoc, convert(varchar, A.ngay_tao, 103) as ngay_tao_vn  ");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }


    protected void btn_src_Click(object sender, EventArgs e)
    {
        //string _sql_src = "";
        if (radio_src_today.Checked)
        {
            sql_where = " ngay_tao = '" + DateTime.Now + "' ";
        }
        if (radio_src_thisweek.Checked)
        {
            string[] _dayinweek = new string[7];
            _dayinweek[0] = DayOfWeek.Sunday.ToString();
            _dayinweek[1] = DayOfWeek.Monday.ToString();
            _dayinweek[2] = DayOfWeek.Tuesday.ToString();
            _dayinweek[3] = DayOfWeek.Wednesday.ToString();
            _dayinweek[4] = DayOfWeek.Thursday.ToString();
            _dayinweek[5] = DayOfWeek.Friday.ToString();
            _dayinweek[6] = DayOfWeek.Saturday.ToString();
            int _vitri_ngay_truoc = 0;
            int _vitri_ngay_sau = 0;
            for (int i = 1; i <= _dayinweek.Length; i++)
            {
                if (_dayinweek[i - 1].Equals(DateTime.Now.DayOfWeek.ToString()))
                {
                    _vitri_ngay_truoc = i;
                    _vitri_ngay_sau = 7 - i;
                }
            }
            sql_where = " ngay_tao between '" + DateTime.Now.AddDays(-_vitri_ngay_truoc) + "' and '" + DateTime.Now.AddDays(_vitri_ngay_sau) + "' ";
        }
        if (radio_src_thismonth.Checked)
        {
            sql_where = " month(ngay_tao) = month(getdate()) and year(ngay_tao) = year(getdate()) ";
        }
        if (radio_src_range.Checked)
        {
            if (txt_src_to_RadDatePicker.SelectedDate.HasValue && txt_src_from_RadDatePicker.SelectedDate.HasValue)
            {
                sql_where = " ngay_tao between '" + txt_src_from_RadDatePicker.SelectedDate.Value + "' and '" + txt_src_to_RadDatePicker.SelectedDate.Value + "' ";
            }
        }
        LoadKhoadaotao();
    }
}
